package com.eeprint.system.service.tenant.impl;


import com.eeprint.common.utils.IDUtil;
import com.eeprint.system.domain.tenant.TenantPrintJob;
import com.eeprint.system.domain.tenant.TenantPrinter;
import com.eeprint.system.mapper.tenant.TenantDocumentMapper;
import com.eeprint.system.mapper.tenant.TenantPrintJobMapper;

import com.eeprint.system.domain.tenant.vo.TenantPrintJobMessage;
import com.eeprint.system.mapper.tenant.TenantPrinterMapper;
import com.eeprint.system.mapper.tenant.TenantUserMapper;
import com.eeprint.system.service.tenant.TenantPrintJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author wsen
 * @Date 2020/10/18 23:37
 * @Version 1.0
 */
@Service
public class TenantPrintJobServiceImpl implements TenantPrintJobService {
    @Autowired
    TenantPrintJobMapper tenantPrintJobMapper;

    @Autowired
    TenantDocumentMapper tenantDocumentMapper;

    @Autowired
    TenantPrinterMapper tenantPrinterMapper;

    @Autowired
    TenantUserMapper tenantUserMapper;
    /**
     * @Author: wsen on 2020/10/19 14:46
     * @Description: 添加打印作业
     */

    @Override
    public int addPrintJob(TenantPrintJob tenantPrintJob) {
        /** 计算出打印张数= 打印份数 x 打印页数 */
        int printPaperCount = tenantPrintJob.getPrintCopyCount()*tenantPrintJob.getPrintPageCount();
        tenantPrintJob.setId(IDUtil.getId());
        tenantPrintJob.setPrintPaperCount(printPaperCount);
        tenantPrintJob.setCreateAt(new Date());
        tenantPrintJob.setPrintStatus(1);
        return tenantPrintJobMapper.insert(tenantPrintJob);
    }

    /**
     * @Author: wsen on 2020/10/19 14:46
     * @Description: 根据员工id查出其所有打印作业信息
     */
    @Override
    public List<TenantPrintJobMessage> list(BigInteger createUserId) {
        List<TenantPrintJob> tenantPrintJobs = tenantPrintJobMapper.selectByCreateUserId(createUserId);
        /** 根据打印作业的信息产生显示的信息 */
        List<TenantPrintJobMessage> collect = tenantPrintJobs.stream().map(pj -> {

            /** 根据对应的id查出对应的名字 */
            String documentName = tenantDocumentMapper.selectNameById(pj.getRelDocumentId());
            String printerName = tenantPrinterMapper.selectNameById(pj.getRelPrinterId());
            String userNick = tenantUserMapper.selectNameById(pj.getCreateUserId());
            /** 赋值操作 */
            TenantPrintJobMessage printJobMessage = new TenantPrintJobMessage(
                    documentName,pj.getPrintCode(),pj.getPrintCopyCount(),
                    pj.getPrintPageStart(),pj.getPrintPageEnd(),pj.getPrintPageCount(),
                    printerName,pj.getPrintStatus(),userNick,pj.getPrintStartTime()
            );

            return printJobMessage;
        }).collect(Collectors.toList());
        return collect;
    }
}
